<?php
Zend_Session::start();
/**
 *  @copyright 2011
 *  @package _version@
 *  @author oriotiori
 */

class LoginController extends Zend_Controller_Action
{

	public function init(){
		$this->view->breadCrumbs = $this->view->crumbs();
		$this->view->breadCrumbs->addPrepend($this->view->baseUrl().'/', 'home')
		->setMvc(true)
		->setGenerateModule(false)
		->setGenerateController(false)
		->setGenerateAction(false);
	}

	public function indexAction(){

		$s_Sass = new Zend_Session_Namespace('s_Sass');
		$Cart = new Zend_Session_Namespace('Cart');
		$request  = $this->getRequest();
		$form = new Default_Form_LoginForm();
		$facadeUsers = new Default_Model_Facade_Users();
		
		$this->view->breadCrumbs
		->addAppend($this->view->baseUrl().'/login/','Login');
		$this->view->crumbs = $this->view->breadCrumbs->init();

		if($this->_request->isPost()){

			$data=$this->_request->getPost();

			$authAdapter = $this->getAuthAdapter();
			$username = (!empty($data['Username'])) ? $data['Username'] : '0000';
			$password = $data['Password'];

			$authAdapter->setIdentity($username)->setCredential(md5($password));

			$auth = Zend_Auth::getInstance();
			$result = $auth->authenticate($authAdapter);

			if($result->isValid()){
				
				$identity = $authAdapter ->getResultRowObject();
				$authStorage = $auth ->getStorage();
				$authStorage->write($identity);				
				
				if(isset($s_Sass->redirectafterloginorregister) && !empty($s_Sass->redirectafterloginorregister)){
					$this->_redirect($s_Sass->redirectafterloginorregister);
				}
				else{ 
					$this->_redirect($this->view->baseUrl() .'/users/myaccount/');
				}

			} else{
				$s_Sass->messageError= array("Autentificacion fallada: combinación usuario-contraseña incorrecta");
				$this->_redirect('login');
			}
		}

		if(Zend_Auth::getInstance()->hasIdentity()){

			if($s_Sass->Role=="3"){
				if(isset($s_Sass->redirectafterloginorregister) && !empty($s_Sass->redirectafterloginorregister) && !preg_match("/admin\//i", $s_Sass->redirectafterloginorregister)){
					$this->_redirect($s_Sass->redirectafterloginorregister);
				}
				$this->_redirect($this->view->baseUrl() .'/users/myaccount/');
			}
			else{ $this->_redirect($this->view->baseUrl() .'admin/');
			}

		}

		if(is_array($s_Sass->messageError)){
			$this->view->messageError= $s_Sass->messageError;
			$s_Sass->messageError='';
		}
		$this->view->loginForm=$form;
	}

	/****
	 * Recupera tu password
	* ****/
	public function forgotyourpasswordAction()
	{
		$s_Sass     = new Zend_Session_Namespace('s_Sass');
		$conf       = new Zend_Session_Namespace('b_Config');
		$request  = $this->getRequest();

		$this->view->breadCrumbs
					->addAppend($this->view->baseUrl().'/login/','Login')
					->addAppend($this->view->baseUrl().'/forgotyourpassword/','¿Olvidó su contraseña?');
		$this->view->crumbs = $this->view->breadCrumbs->init();
		$Facade =   new Default_Model_Facade_Login();
		$To =       new Default_Model_TO_Login();
		

		if($this->_request->isPost()){
			
			$data=$this->_request->getPost();

			$To->setEmail($data['Email']);
			$To->setIP($request->getServer('REMOTE_ADDR'));
			$To->setPassword($Facade->createRandPass(8,true,true,false));
			
			if($Facade->validateEmailExist($To->getAllVars())){
				if($Facade->RecoveryPassword($To->getAllVars())){
					$s_Sass->messageError[]='Acabamos de enviar a su email una nueva contraseña.';
					$this->_redirect($this->view->baseUrl() .'/login/');
				}
			}
			
		}
		if(is_array($s_Sass->messageError)){
			$this->view->messageError = $s_Sass->messageError;
			$s_Sass->messageError='';
		}
	}
	

	public function logoutAction()
	{
		$s_Sass = new Zend_Session_Namespace('s_Sass');
		$s_Sass->unsetAll();
		$Cart = new Zend_Session_Namespace('Cart');
		$Cart->unsetAll();
		
		Zend_Auth::getInstance()->clearIdentity();
		$this->_redirect($this->view->baseUrl());
	}
	
	
	public function getAuthAdapter()
	{
		$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
		$authAdapter->setTableName(TABLE_PREFIX.'users')
					->setIdentityColumn('Username')
					->setCredentialColumn('Password');

		return $authAdapter;
	}


}



